package com.ysd.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ysd.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public static String app_id = "2016101500695860";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCv5GgSE9Q+ggRNeJVu9ovSnOwLTKCH+eOL5aK4RVFn2zRtli0imLtJTY69LXK0y2zbBF0gqL2inkXwoOPkHSaACgRo/uVTspxHF17iGFutV+hOe8WgDeFidVwEle7goug/OmN1bIJcQ0sU5yZjNinafstoqz8h/FYmTgzZ12KSGZUXl5vs2shSKBAyZrHsZQFD7NY2ZaAwWRYoHi5h18ZZRtvAjHtWnL7Y1r8a7QrvkEK2SidUaa+x8BKD7VCiSRJnOE1t2hLTgtnzu7Nd2AeqfybkbTYFV0l84AFWRxace9+H4VM4/A8tbQqKAzRNxRXPrUuiiFe2EWh777bNi/35AgMBAAECggEAUIjdGcHYgVJYk/wtN6RNOv68mSPZsMi65hgbnBGpB0t3wuFrAxc5UYPgYkAH8D3gNpvV5Bk+0tFzJo0UKyMpuKBdipGDAQKbyXUq7YzHlSPV6vGC+BjC5Kjlm/fmaxZMDn82gNJREduB6i04B18KZM9/ZQpbmr0F2P4NpkSXDltB68aQgewwecpEN4WNQBWrdGKU6yLi3KOnyWaA+ASG40B9jG59ud2InOUvRJUVPLf/ApFn+t2OTlZIFx9FNaaCxG64MuFV/zSmov44NyJLHLJnx3wrVzvm0glG34tlKKjRYcw1YaHeSf7et5OAewdpNRfZOW7vXRjvhTyk9V5/fQKBgQD1P5dJjEVIFqFvkwe+Pfp2ugFIgDTdOK7axQDYbpFj8etj5O7ithlYRkyWGBNKmfoJRAgSXj8SnRu0TTzlE6H0KfQUjt0g1rDyQmVVMg+tKh8axAlJAG/EjDy0gHp+Ur6TsC6RzLacioMADVKnzE+hBho2flMft+Qw2+2WlOR3GwKBgQC3mm9YpMcQilUEgmIlAQ0rZ8B1i3eY66X/LlZUmz6Lfhk/5sY2a3AFnCxdf3k0IcYjhBXIUE3WinaWfc/Wl5Zm0xBud+Y4BEmx6geE9/NMVuq+8cRZPF1UATalToPxyjaPdxwiF8qzEySGmORz+2pP76zPdquk3p3cH/ofLiyMewKBgEQ/xBxFmuoq0hY4tojsuozbyougkhWWNqLO/CM+8dr8ceNEzG3u+zKjraB/Zq72Fq0Jb62c6LSHYDRW4kFlq0ZFfpVSBJuk420nqQeCfPZVqH5MW8EjC/eIh0Q8FNqesEZtWyhb7d22dipHit8Dbo2DIzxS/1LuW+R077NeEA5NAoGADC/GAD4/sEteRURaO1aMhzsFvUeakp3hkMSEmm0Lg7rE9kbdFTOgZd7t2u7hHqK3Qhg8UeVypTDfmjGToAzDwON1jb+LmmA+e/6qrRrAHWsPfckSXVHyZ+cEt4g0ndHsOS2QwnqNy8EThZOUZlZjwhaIg+S+k9zq4XeF5JfuAQ0CgYEAvdupbkBgIf9UBQCF9LZpuCdq1Oe6JghNq8kSPfvf04Jdz2qCeCjqunvsakv13nWifDdVtXTxa5jsv2kWYY0Qty4rfdmdaMS6r4yE8NY8NJ3AtGlf0q1u/5hSa3GnasiwfKGeqLU85F1l4Ud/0n4Q7gQ5tztI8WaQpv9+s0qpWgk=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAknLJZRhLkpBdcRxt+DZINQ4u4aSWqhsyzC424moF1Blh6/gx3NBrg1t6lPQrEeg63KAr+uovFHL29OL5Tn+T3XWSEJYtieM1XPfykk1KRp2dmWkJ9E/oPYe8GP86y6Y+dEhPCfu8H1YDRtiY/mkTYMT+XdePKC1qB2bOe2DvlMz217uaRetY8n3hXiKZ7FydTFJ8quah4fzoZQhanjXz31QrdVuaiPp+eq6KzrYOS+qXF8SfT6UVVHp5f0uakGFCOul9EHPlh4a4gmFttNqeKfgEz63VYvkT/JNSTROYK7DEmStAWr8gD6QvPfCO41zbB5BxcqqO6JKEhLEXZCY5UQIDAQAB";

    // 服务器异步通知页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String return_url = "http://localhost:9000/memberOrder.html";


    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
